<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>

      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.carNumber"
      />
      <span class="search-label">车主姓名：</span>

      <el-input
        clearable
        placeholder="请输入内容"
        class="search-main"
        v-model="params.personName"
      />
      <span class="search-label">状态：</span>

      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in cardStatusList"
          :key="item.id"
          :value="item.id"
          :label="item.name"
        />
      </el-select>

      <el-button type="primary" class="search-btn" @click="getCardList"
        >查询</el-button
      >
    </div>

    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <!-- 添加月卡,跳转到新增月卡页面 -->
      <el-button type="primary" @click="$router.push('/cardAdd')"
        >添加月卡</el-button
      >

      <el-button @click="delCartList">批量删除</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="cardList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />

        <el-table-column
          label="状态"
          prop="cardStatus"
          :formatter="formatStatus"
        >
          <template slot-scope="scope">
            <el-tag
              :type="getStatusType(scope.row.cardStatus)"
              disable-transitions
            >
              {{ formatStatus(scope.row) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text" @click="renewCard(scope.row)">续费</el-button>

            <el-button size="mini" type="text" @click="showCardDetail(scope.row.id)">查看</el-button>

            <el-button size="mini" type="text" @click="editCard(scope.row.id)"
              >编辑</el-button
            >

            <el-button size="mini" type="text" @click="deleteCard(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </div>
    <!-- 查看详情 -->
    <el-dialog title="查看详情" :visible.sync="showDialog" width="75%">
    <el-form label-width="120px">
      <el-form-item label="车主姓名">
        <span>{{ detailedCardInfo.personName }}</span>
      </el-form-item>
      <el-form-item label="联系方式">
        <span>{{ detailedCardInfo.phoneNumber }}</span>
      </el-form-item>
      <el-form-item label="车牌号码">
        <span>{{ detailedCardInfo.carNumber }}</span>
      </el-form-item>
      <el-form-item label="车辆品牌">
        <span>{{ detailedCardInfo.carBrand }}</span>
      </el-form-item>
      <el-form-item label="充值记录">
        <el-table :data="rechargeList" border style="width: 100%">
          <el-table-column label="创建用户" prop="createUser"></el-table-column>
          <el-table-column label="充值 ID" prop="rechargeId"></el-table-column>
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="卡开始日期" prop="cardStartDate"></el-table-column>
          <el-table-column label="卡结束日期" prop="cardEndDate"></el-table-column>
          <el-table-column label="支付方式" prop="paymentMethod"></el-table-column>
          <el-table-column label="支付金额" prop="paymentAmount"></el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="showDialog = false">关闭</el-button>
    </span>
  </el-dialog>

    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>

          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>

          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>

          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>

        </el-form> -->
      </div>

      <template #footer>
        <el-button size="mini">取 消</el-button>

        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCarListAPI, delCardAPI, delCardListAPI,getCardInfoDetailAPI } from "@/api/carCard";
import { Message } from "element-ui";
export default {
  data() {
    return {
      // 请求参数
      params: {
        page: 1,
        pageSize: 6,
        carNumber: null,
        personName: null,
        cardStatus: null,
      },
      cardList: [], // 月卡列表
      total: 0, //总条数
      cardStatusList: [
        { id: null, name: "全部" },
        { id: 0, name: "可用" },
        { id: 1, name: "已过期" },
      ],
      // 详细月卡信息
      showDialog: false,
      detailedCardInfo: {},
      //详细月卡充值信息
      rechargeList: [
        {
          "createUser": "string",
          "rechargeId": 0,
          "createTime": "string",
          "cardStartDate": "string",
          "cardEndDate": "string",
          "paymentMethod": "Cash",
          "paymentAmount": 0
        }
      ]
    };
  },
  mounted() {
    this.getCardList();
  },
  methods: {
    //获取月卡列表
    async getCardList() {
      const res = await getCarListAPI(this.params);
      this.cardList = res.data.rows;
      this.total = res.data.total;
      // console.log(this.cardList);
    },
    //状态的启用
    formatStatus(row) {
      const MAP = {
        0: "可用",
        1: "已过期",
      };
      return MAP[row.cardStatus];
    },
    //为标签设置不同的状态控制样式
    getStatusType(status) {
      // 根据不同的 cardStatus 值返回不同的标签类型
      // console.log(status);
      if (status == "0") {
        return "success";
      }
      if (status == "1") {
        return "danger";
      }
    },
    //编辑月卡
    editCard(id) {
      this.$router.push({
        path: "/cardEdit",
        query: {
          id,
        },
      });
    },
    //分页
    pageChange(val) {
      //点击的页数赋值给请求参数页数
      this.params.page = val;
      // 重新请求数据
      this.getCardList();
    },
    //删除月卡
    async deleteCard(id) {
      // console.log(id);
      this.$confirm("此操作将永久删除该月卡, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          const res = await delCardAPI(id);
          Message.success(res.msg);
          this.getCardList();
        } catch (err) {
          Message.error(err.msg);
        }

        // console.log(res);
      });
    },
    //批量删除数组
    handleSelectionChange(rowList) {
      console.log(rowList);
      this.selectedCarList = rowList;
    },
    //批量删除月卡
    delCartList() {
      this.$confirm("此操作将永久删除选择的月卡, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 处理id
          await delCardListAPI(this.selectedCarList.map((item) => item.id));
          this.getCardList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //查看月卡详情
    async showCardDetail(id) {
      // console.log(id);
      const res = await getCardInfoDetailAPI(id);
      // console.log(res);
      this.detailedCardInfo = res.data;
      this.rechargeList = res.data.rechargeList;
      this.showDialog = true;
    },
    //点击续费跳转到新增月卡界面,车辆信息回显,而且输入框状态不可更改
    renewCard(info) {
      // console.log(info);
      this.$router.push({
        path: "/cardRenew",
        query: {
          info:JSON.stringify(info)
        },
      });
    },
  },
};
</script>


<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
